﻿using System.Runtime.CompilerServices;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Validate;

namespace Shop.RemoteModel.LogisticsConfig.Model
{
    public class LogisticsConfigSet
    {
        /// <summary>
        /// 估价方式 
        ///</summary>
        [EnumValidate("shop.logistics.valuation.mode.error", typeof(ValuationMode))]
        public ValuationMode ValuationMode { get; set; }

        /// <summary>
        /// 起始值 
        ///</summary>
        [NumValidate("shop.logistics.first.val.error", 0)]
        [EntrustValidate("_CheckConfig")]
        public decimal FirstVal { get; set; }

        /// <summary>
        /// 物流费用 
        ///</summary>
        [NumValidate("shop.logistics.price.error", 1)]
        public decimal LogisticsPrice { get; set; }

        /// <summary>
        /// 续 
        ///</summary>
        [NumValidate("shop.logistics.continue.val.error", 0)]
        public decimal ContinueVal { get; set; }

        /// <summary>
        /// 费用 
        ///</summary>
        [NumValidate("shop.logistics.continue.price.error", 0)]
        public decimal ContinuePrice { get; set; }

        /// <summary>
        /// 免物流费用最低值 
        ///</summary>
        [NumValidate("shop.logistics.free.value.error", 0)]
        public decimal FreeValue { get; set; }

        /// <summary>
        /// 免物流费用最低金额 
        ///</summary>
        [NumValidate("shop.logistics.free.price.error", 0)]
        public decimal FreePrice { get; set; }

        /// <summary>
        /// 立方厘米比例值
        /// </summary>
        [NumValidate("shop.logistics.cc.value.error", 0)]
        public short CcValue { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [LenValidate("shop.logistics.remark.len", 0, 50)]
        public string Remark { get; set; }

        /// <summary>
        /// 是否启用 
        ///</summary>
        public bool IsEnable { get; set; }

        [MethodImpl(MethodImplOptions.NoOptimization)]
        private void _CheckConfig ()
        {
            if (this.ValuationMode == ValuationMode.固定金额)
            {
                return;
            }
            else if (this.FirstVal == 0)
            {
                throw new ErrorException("shop.logistics.first.val.zero");
            }
            else if (this.ContinueVal != 0 && this.ContinuePrice == 0)
            {
                throw new ErrorException("shop.logistics.continue.price.zero");
            }
            else if (( this.ValuationMode == ValuationMode.计体积 || this.ValuationMode == ValuationMode.计重和体积 ) && this.CcValue == 0)
            {
                throw new ErrorException("shop.logistics.cc.value.zero");
            }
        }
    }
}
